from appium import webdriver
from appium.webdriver.common.mobileby import MobileBy       # 导入元素操作模块
from time import sleep
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from appium.webdriver.common.touch_action import TouchAction        # 导入手势事件
import pytest
import allure
@allure.feature("存钱罐")
class TestCunQ:
    @allure.story("前置方法")
    def setup_method(self):
        with allure.step("实例化驱动"):
            # 实例化驱动
            dict_p = {}
            dict_p['deviceName'] = '127.0.0.1:62001'        # 可以通过adb devices -l 来获取
            dict_p['platformName'] = 'Android'      # Android或者iOS
            dict_p['platformVersion'] = '7.1.2'
            dict_p['appPackage'] = 'com.lingyue.zebraloan'       # adb shell dumpsys activity activities
            dict_p['appActivity'] = 'com.lingyue.banana.activities.BananaLaunchActivity'
            self.driver = webdriver.Remote('http://localhost:4723/wd/hub',dict_p)
    @allure.story("后置方法")
    def teardown_method(self):
        with allure.step("退出当前窗口"):
            sleep(10)
            self.driver.quit()      # 退出
    @allure.story("用户故事")
    def test_userStory(self):
        with allure.step("点击同意按钮"):
            WebDriverWait(self.driver,10,0.5).until(EC.presence_of_element_located((MobileBy.ID,'com.lingyue.zebraloan:id/tv_confirm'))).click()
        with allure.step("等待回到主页"):
            sleep(10)
        with allure.step("点击用户故事"):
            WebDriverWait(self.driver,10,0.5).until(EC.presence_of_all_elements_located((MobileBy.CLASS_NAME,'android.widget.ImageView')))[5].click()
        with allure.step("等待进入用户故事"):
            sleep(7)
        with allure.step("点击第一个故事"):
            TouchAction(self.driver).tap(x=432,y=497).perform()
        with allure.step("等待页面加载"):
            sleep(4)
        with allure.step("向下滑动用户故事内容"):
            TouchAction(self.driver).press(x=470,y=1508).move_to(x=450,y=414).release().perform()
        with allure.step("点击返回按钮"):
            WebDriverWait(self.driver,10,0.5).until(EC.presence_of_element_located((MobileBy.ID,'com.lingyue.zebraloan:id/tb_toolbar'))).click()
        with allure.step("等待回到首页"):
            sleep(2)
        with allure.step("获取文本进行断言"):
            text = WebDriverWait(self.driver,10,0.5).until(EC.presence_of_all_elements_located((MobileBy.ID,'com.lingyue.zebraloan:id/tv_title')))[-1].text
        with allure.step(f"断言：{text} 是否出现"):
            assert text == '3步借款 申请便捷'
"""
报告生成：
pytest [测试文件] --alluredir=allure
allure generate ./allure/ -o ./reports --clean
"""